package org.zyx.goods.service;

import org.springframework.stereotype.Service;
import org.zyx.goods.client.BrandClient;
import org.zyx.goods.client.CategoryClient;
import org.zyx.goods.client.GoodsClient;
import org.zyx.goods.client.SpecificationClient;
import org.zyx.item.entity.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: SwordMaple
 * @Date: 2020/10/11  23:51
 * @Description:
 * @Version: 1.0
 */
@Service
public class GoodsService {

    @Resource
    private CategoryClient categoryClient;
    @Resource
    private BrandClient brandClient;
    @Resource
    private SpecificationClient specificationClient;
    @Resource
    private GoodsClient goodsClient;

    /**
     * 组合数据模型
     * @return
     */
    public Map<String,Object> loadData(Long spuId){
        Map<String,Object> modelMap = new HashMap();

        //1.根据spuId查询spu
        Spu spu = goodsClient.findSpuById(spuId);
        //2.查询spuDetail
        SpuDetail detail = goodsClient.findSpuDetailById(spuId);
        //3.查询分类:Map<String,Object>
        List<Long> cids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        List<String> names = categoryClient.queryNameByIds(cids);
            //3.1初始化分类的集合
        List<Map<String, Object>> categories = new ArrayList<>();
        for (int i = 0; i < cids.size(); i++) {
            //3.2使用map存储id,name
            Map<String, Object> map = new HashMap<>();
            map.put("id",cids.get(i));
            map.put("name",names.get(i));
            categories.add(map);//添加到List中
        }
        //4.查询品牌
        Brand brand = brandClient.findBrandById(spu.getBrandId());
        //5.查询sku集合
        List<Sku> skus = goodsClient.findSkusBySpuId(spuId);
        //6.查询规格参数组
        List<SpecGroup> groups = specificationClient.findGroupsWithParam(spu.getCid3());
        //7.查询特殊的规格参数,初始化特殊规格参数的map:id,value
        Map<Long,String> paramMap = new HashMap<>();
        List<SpecParam> params = specificationClient.findParams(null, spu.getCid3(), false, null);
        params.forEach(param -> {
            paramMap.put(param.getId(),param.getName());
        });
        //将所有参数添加到map集合中
        modelMap.put("spu",spu);  //商品集
        modelMap.put("spuDetail",detail);//商品详情
        modelMap.put("categories",categories);//分类集
        modelMap.put("brand",brand);//品牌对象
        modelMap.put("skus",skus);//sku集合
        modelMap.put("groups",groups);//参数分组
        modelMap.put("paramMap",paramMap);//参数map集合

        return modelMap;
    }



}
